<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2020/4/24
 * Time: 10:12
 */
namespace app\admin\controller;
use think\Db;
use think\Request;

class Musics extends Base{
    public function index(){
        if(request()->isAjax()){
            $map = array();
            extract(input());
            if(isset($keyword)&&$keyword!="")
            {
                $map['uuu.user_username|uuu.user_phone'] = $keyword;
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('record_music')->alias('a')
                ->join('up_u_user uuu','uuu.user_id = a.user_id','left')
                ->join('up_music uv','uv.music_id = a.video_id','left')
                ->where($map)->count();// 获取总条数
            $lists = Db::name('record_music')->alias('a')
                ->join('up_u_user uuu','uuu.user_id = a.user_id','left')
                ->join('up_music uv','uv.music_id = a.video_id','left')
                ->where($map)
                ->page($NowPage,$limits)
                ->field('uuu.user_username,uuu.user_phone,a.*,uv.music_name')
                ->order('record_video_id desc')
                ->select();
            $count2 = Db::name('record_music')->alias('a')
                ->join('up_u_user uuu','uuu.user_id = a.user_id','left')
                ->join('up_music uv','uv.music_id = a.video_id','left')
                ->where($map)
                ->column('record_video_id');
            $str = '';
            if($count2){
                $str = implode($count2,',');
            };

//            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists,'count2'=>$str]);

        }
        return $this->fetch();
    }
    /**
     * 导出
     * @return \think\response\Json
     */
    public function outExcel(){
        $order_ids = input('ids');
        if($order_ids == ''){
            return json(['code' => 100, 'msg' => '请选择要操作的数据']);
        }
        $map['record_video_id'] = array('IN',$order_ids);
        $lists = Db::name('record_music')->alias('a')
            ->join('up_u_user uuu','uuu.user_id = a.user_id','left')
            ->join('up_music uv','uv.music_id = a.video_id','left')
            ->where($map)
            ->field('uuu.user_username,uuu.user_phone,uv.music_name,a.video_num,a.video_time')
            ->order('record_video_id desc')
            ->select();
        $cellname = [
            ['user_username','用户名',20,'LEFT'],
            ['user_phone','手机号',20,'LEFT'],
            ['music_name','音频名称',20,'LEFT'],
            ['video_num','观看量',20,'LEFT'],
            ['video_time','用户观看退出时间',20,'LEFT'],
        ];
        $res = exportExcel('用户音频观看记录','member',$cellname,$lists);
        return json($res);

    }

}